/**
 * Copyright 2014  XCL-Charts
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 	
 * @Project XCL-Charts 
 * @Description Android图表基类库
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 * @Copyright Copyright (c) 2014 XCL-Charts (www.xclcharts.com)
 * @license http://www.apache.org/licenses/  Apache v2 License
 * @version 1.0
 */
package com.shengde.jinwawaforleader.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;


/**
 * @ClassName DountChart01View
 * @Description  环形图例子
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 */
public class MyChatView extends View {
	private static final String TAG = "MyChatView";
	//分配比例大小，总比例大小为100,由于经过运算后最后会是99.55左右的数值，导致圆不能够重合，会留出点空白，所以这里的总比例大小我们用101
//	private int[] strPercent = new int[]{10, 25, 18, 41, 2, 5};

	//圆的直径
	private float mRadius = 250;
	//圆的粗细
	private float mStrokeWidth = 60;
	//文字大小
	private int textSize = 20;
	//-------------画笔相关-------------
	//圆环的画笔
	private Paint cyclePaint;
	//文字的画笔
	private Paint textPaint;
	//标注的画笔
	private Paint labelPaint;
	//-------------颜色相关-------------
	//边框颜色和标注颜色
	private int[] mColor = new int[]{0xFF71a9fe, 0xFFfc7475, 0xFFfed06e};
	//文字颜色
	private int textColor = 0xFF000000;
	//-------------View相关-------------
	//View自身的宽和高
	private int mHeight;
	private int mWidth;
	//-------------必须给的数据相关-------------
	public ArrayList<String> str = new ArrayList<>();

	public void setStr(ArrayList<String> str) {
		this.str = str;
	}
	public ArrayList<Integer> strPercent = new ArrayList();
	public void setStrPercent(ArrayList<Integer> strPercent) {
		this.strPercent = strPercent;
	}

	public MyChatView(Context context) {
		super(context);
	}

	public MyChatView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyChatView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mWidth = w;
		mHeight = h;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.i(TAG, "onDraw: 123465452132");
		//移动画布到圆环的左上角
		canvas.translate(mWidth / 2 - 150, mHeight / 2 - 100);
//		canvas.translate(mWidth, mHeight);
		//初始化画笔
		initPaint();
		//画圆环
		drawCycle(canvas);
		//画文字和标注
//		drawTextAndLabel(canvas);
		Log.i(TAG, "onDraw: 4444444444444444");
	}

	/**
	 * 初始化画笔
	 */
	private void initPaint() {
		//边框画笔
		cyclePaint = new Paint();
		cyclePaint.setAntiAlias(true);
		cyclePaint.setStyle(Paint.Style.STROKE);
		cyclePaint.setStrokeWidth(mStrokeWidth);
		//文字画笔
		textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setColor(textColor);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setStrokeWidth(1);
		textPaint.setTextSize(textSize);
		//标注画笔
		labelPaint = new Paint();
		labelPaint.setAntiAlias(true);
		labelPaint.setStyle(Paint.Style.FILL);
		labelPaint.setStrokeWidth(2);
		Log.i(TAG, "initPaint: 1111111");
	}

	/**
	 * 画圆环
	 *
	 * @param canvas
	 */
	private void drawCycle(Canvas canvas) {
		float startPercent = 0;
		float sweepPercent = 0;
		for (int i = 0; i < strPercent.size(); i++) {
			cyclePaint.setColor(mColor[i]);
			startPercent = sweepPercent + startPercent;
			//这里采用比例占100的百分比乘于360的来计算出占用的角度，使用先乘再除可以算出值
			sweepPercent = strPercent.get(i) * 360 / 100;
			canvas.drawArc(new RectF(0, 0, mRadius, mRadius), startPercent, sweepPercent, false, cyclePaint);
		}

		Log.i(TAG, "drawCycle: 222222222222222");
	}

	/**
	 * 画文字和标注
	 *
	 * @param canvas
	 */
	private void drawTextAndLabel(Canvas canvas) {
		for (int i = 0; i < strPercent.size(); i++) {
			//文字离右边环边距为60，文字与文字之间的距离为40
			canvas.drawText(str.get(i), mRadius + 60, i * 40, textPaint);
			//画标注,标注离右边环边距为40,y轴则要减去半径（10）的一半才能对齐文字
			labelPaint.setColor(mColor[i]);
			canvas.drawCircle(mRadius + 40, i * 40 - 5, 10, labelPaint);
		}
		Log.i(TAG, "drawTextAndLabel: 333333333333");
	}

}